//
//  UserMessageViewController.m
//  TengyunStation
//
//  Created by Johnny on 15/9/7.
//  Copyright (c) 2015年 xingrong. All rights reserved.
//

#import "UserMessageViewController.h"
#import "UserMessageCell.h"
#import "Singleton.h"
#import "TSHeader.h"
#import "ZXingObjC.h"
#import "UIImage+QRCode.h"

@interface UserMessageViewController ()

@end

@implementation UserMessageViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title = @"我的小站";
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    _websitaNameLabel.text = [Singleton shareInstance].userMessage.websiteName;
    _headerView.frame = CGRectMake(0, 0, 320, 123);
    _starView.scorePercent = [Singleton shareInstance].userMessage.websiteGrade/5.0;
    self.tableView.tableHeaderView = _headerView;
    if ([Singleton shareInstance].userMessage.websiteQcode.length > 0) {
        _footerView.frame = CGRectMake(0, 0, 320, 170);
        _footerView.layer.borderWidth = 0.5;
        _footerView.layer.borderColor = LineColor.CGColor;
        self.tableView.tableFooterView = _footerView;
        if (IOS7AndLater) {
            _qcodeImageView.image = [UIImage qrImageForString:[Singleton shareInstance].userMessage.websiteQcode ImageSizeWidth:130];
        } else {
            ZXMultiFormatWriter *writer = [[ZXMultiFormatWriter alloc] init];
            ZXBitMatrix *result = [writer encode:[Singleton shareInstance].userMessage.websiteQcode format:kBarcodeFormatQRCode width:130 height:130 error:nil];
            if (result) {
                CGImageRef imageRef = [[ZXImage imageWithMatrix:result] cgimage];
                UIImage *image = [UIImage imageWithCGImage:imageRef];
                _qcodeImageView.image = image;
            }
        }
    }
}

#pragma mark UITableViewDataSource,UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 4;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 10;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    if ([Singleton shareInstance].userMessage.websiteQcode.length > 0) {
        return 10;
    }
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIndentifier = @"UserMessageCell";
    UserMessageCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIndentifier];
    if (!cell) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"UserMessageCell" owner:self options:nil] lastObject];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    if (indexPath.row == 0) {
        cell.nameLabel.text = @"站点编号";
        cell.detailLabel.text = [Singleton shareInstance].userMessage.websiteCode;
    } else if (indexPath.row == 1) {
        cell.nameLabel.text = @"负责人";
        cell.detailLabel.text = [Singleton shareInstance].userMessage.userName;
    } else if (indexPath.row == 2) {
        cell.nameLabel.text = @"联系电话";
        cell.detailLabel.text = [Singleton shareInstance].userMessage.telephone;
    } else if (indexPath.row == 3) {
        cell.nameLabel.text = @"签约时间";
        cell.detailLabel.text = [Singleton shareInstance].userMessage.registerTime;
    }
    return cell;
}

@end
